package com.yx.cloud.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.View;

import com.yx.cloud.R;
import com.yx.cloud.base.BaseActivity;
import com.yx.cloud.databinding.MessgeparticularsActivityBinding;
import com.yx.cloud.utils.Intent_utils;
import com.yx.cloud.utils.RomUtils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

/**
 * 消息详细信息界面
 */
public class MessageParticularsActivity extends BaseActivity implements View.OnClickListener {

    private MessgeparticularsActivityBinding binding;

    @Override
    public void onCreate() {
        binding = MessgeparticularsActivityBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        binding.tvMessageParticularsTitle.setText(title);

        String type = intent.getStringExtra("type");
        binding.tvMessageType.setText(type);

        String content = intent.getStringExtra("content");
        binding.tvMessageContent.setText(content);

        String time = intent.getStringExtra("time");
        binding.tvMessageTime.setText(time);

        binding.tvMessageExtra.setVisibility(View.GONE);

        binding.ivMessageParticularsReturn.setOnClickListener(this);

        String icon = intent.getStringExtra("icon");
        binding.WarningMesIcon.setImageResource(R.mipmap.headportrait_icon);
        loadRobotIcon(icon);

    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        int id = v.getId();
        if (id == R.id.iv_messageParticularsReturn) {
            Intent_utils.backIntent(this);
        }
    }

    private void loadRobotIcon(String icon) {
        AsyncTask<String, Void, Bitmap> asyncTask = new AsyncTask<String, Void, Bitmap>() {
            @Override
            protected Bitmap doInBackground(String... uriArray) {
                try {
                    //这个URI是图片下载到本地后的缓存目录中的URI
                    return getLocalOrNetBitmap(uriArray[0]);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return null;
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                if (bitmap != null) {
                    binding.WarningMesIcon.setImageBitmap(bitmap);
                }
            }
        };
        asyncTask.execute(icon);
    }

    /**
     * @param url 　本地或网络的url
     * @return　url的bitmap
     */
    public Bitmap getLocalOrNetBitmap(String url) {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.headportrait_icon);
        if (url != null) {
            InputStream in = null;
            BufferedOutputStream out = null;
            try {
                //读取图片输入流
                in = new BufferedInputStream(new URL(url).openStream(), 2 * 1024);
                //准备输出流
                final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
                out = new BufferedOutputStream(dataStream, 2 * 1024);
                byte[] b = new byte[1024];
                int read;
                //将输入流变为输出流
                while ((read = in.read(b)) != -1) {
                    out.write(b, 0, read);
                }
                out.flush();
                //将输出流转换为bitmap
                byte[] data = dataStream.toByteArray();
                bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
                data = null;
                return bitmap;
            } catch (IOException e) {
                e.printStackTrace();
                return bitmap;
            }
        }
        return bitmap;
    }
}
